package com.wc.算法提高课.C第三章_图论.最近公共祖先.祖孙询问;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/5/30 0:37
 * @description https://www.acwing.com/problem/content/description/1174/
 */
public class Main {
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 40010, M = N << 1, idx = 1, INF = 0x3f3f3f3f;
    static int[] h = new int[N], ne = new int[M], e = new int[M], depth = new int[N], q = new int[N];
    // f[i][j] 表示第i个点跳2^j次方步跳到达的位置
    static int[][] f = new int[N][16];
    static int n, m;

    public static void main(String[] args) {
        n = sc.nextInt();
        int root = -1;
        for (int i = 1; i <= n; i++) {
            int a = sc.nextInt(), b = sc.nextInt();
            if (b == -1) root = a;
            else {
                add(a, b);
                add(b, a);
            }
        }
        bfs(root);
        m = sc.nextInt();
        while (m-- > 0) {
            int a = sc.nextInt(), b = sc.nextInt();
            int p = lca(a, b);
            if (p == a) out.println(1);
            else if (p == b) out.println(2);
            else out.println(0);
        }
        out.flush();
    }

    static int lca(int a, int b) {
        if (depth[a] < depth[b]) {
            return lca(b, a);
        }
        // a跳到与b相同的位置
        for (int i = 15; i >= 0; i--) {
            if (depth[f[a][i]] >= depth[b]) {
                a = f[a][i];
            }
        }
        if (a == b) return a;
        // 同时往上跳
        for (int i = 15; i >= 0; i--) {
            if (f[a][i] != f[b][i]) {
                a = f[a][i];
                b = f[b][i];
            }
        }
        return f[a][0];
    }

    static void bfs(int root) {
        Arrays.fill(depth, INF);
        depth[root] = 1;
        depth[0] = 0;
        int hh = 1, tt = 0;
        q[++tt] = root;
        while (hh <= tt) {
            int t = q[hh++];
            for (int i = h[t]; i > 0; i = ne[i]) {
                int j = e[i];
                if (depth[j] > depth[t] + 1) {
                    depth[j] = depth[t] + 1;
                    q[++tt] = j;
                    f[j][0] = t;
                    for (int k = 1; k <= 15; k++) {
                        f[j][k] = f[f[j][k - 1]][k - 1];
                    }
                }
            }
        }
    }

    static void add(int a, int b) {
        e[idx] = b;
        ne[idx] = h[a];
        h[a] = idx++;
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
